package il.ac.technion.wiki.api.wikidocument;

import static org.junit.Assert.fail;
import il.ac.technion.wiki.api.wikidocument.content.StringContentImporter;
import il.ac.technion.wiki.api.wikidocument.metadata.Language;

import java.io.InvalidObjectException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class WikiDocumentTest {
	Map<String, String> myMeta = null;
	// That's how you create a document from title and content (string)
	private IWikiDocument doc = new WikiDocument("test", "test"); 
	// That's how you create a document from title and a content importer 
	@SuppressWarnings("unused")
	private IWikiDocument doc2 = new WikiDocument("test2", new StringContentImporter("test")); 
	IWikiDocument doc3 = null; 
	IWikiDocument doc4 = null; 
	
	public WikiDocumentTest() {
		myMeta = new HashMap<String, String>();
		myMeta.put("date", new Date().toString());
		myMeta.put("name", "myContent's name");
		myMeta.put("author", "elad");
		myMeta.put("language", Language.English.toString());
		
		// That's how you create a document from title, a content (string) and a metadata
		doc3 = new WikiDocument("test3", "test", myMeta);
		
		// That's how you create a document from title and a content importer and a metadata
		doc4 = new WikiDocument("test4", new StringContentImporter("test"), myMeta);
	}
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testWikiDocumentV3() {
		try{
			new WikiDocument(null, "");
			fail("Shouldn't be able to create documnet with empty content");
		} catch (Exception e) {}
		try{
			new WikiDocument("myTest", (String)null);
			fail("Shouldn't be able to create documnet with empty content");
		} catch (Exception e) {}
		try{
			new WikiDocument("", "");
			fail("Shouldn't be able to create documnet with empty content");
		} catch (Exception e) {}
		try{
			new WikiDocument("myTest", "");
			fail("Shouldn't be able to create documnet with empty content");
		} catch (Exception e) {}
		try{
			new WikiDocument("myTest", "bla", null);
			fail("Shouldn't be able to create documnet with empty content");
		} catch (Exception e) {}
	}

	@Test
	public void testGetTitle() {
		Assert.assertEquals("test", doc.getTitle());
	}

	@Test
	public void testGetContent() {
		Assert.assertEquals("test", doc.getContent());
	}

	@Test
	public void testUpdateContentString() {
		doc.updateContent("bla");
		Assert.assertEquals("bla", doc.getContent());
		try{
			doc.updateContent((String)null);
			fail("updated null content");
		} catch (Exception e){}
		doc.updateContent(new StringContentImporter("test"));
		Assert.assertEquals("test", doc.getContent());
	}

	@Test
	public void testUpdateMetadata() {
		Map<String, String> newMetadata = new HashMap<String, String>();
		newMetadata.put("author", "kravi");
		doc3.updateMetadata(newMetadata);
		Assert.assertEquals(1, doc3.getCurrentVersion().intValue());
		Map<String, String> currentMetadata = doc3.getCurrentMetadata();
		if (currentMetadata == null)
			fail("doc3 metadata is null");
		Assert.assertEquals("kravi", currentMetadata.get("author"));
	}

	@Test
	public void testGetCurrentMetadata() {
		Map<String, String> meta = doc.getCurrentMetadata();
		if (meta != null)
			fail("meta should be null");
		meta = doc3.getCurrentMetadata();
		Assert.assertEquals("elad", meta.get("author"));
		Assert.assertEquals(null, meta.get("someMetaNotExists"));
	}

	@Test
	public void testGetCurrentVersion() {
		IWikiDocument test = new WikiDocument("testGetCurrentVersion", "bla");
		Assert.assertEquals(0, test.getCurrentVersion().intValue());
		test.updateContent("bla2");
		Assert.assertEquals(1, test.getCurrentVersion().intValue());
	}

	public void testBackToVersion() {
		IWikiDocument test = new WikiDocument("testGetCurrentVersion", "bla", this.myMeta);
		test.updateContent("bla2");
		try {
			test.backToVersion(-1);
			fail("backed to version -1");
		} catch (InvalidObjectException e) {}
		try {
			test.backToVersion(0);
			Assert.assertEquals("bla", test.getContent());
		} catch (InvalidObjectException e) {
			fail("backed to version 0");
		}
		Assert.assertEquals("elad", test.getCurrentMetadata().get("author"));
	}
}
